package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.config.RespCode;
import com.workManager.service.ProjectCostService;
import com.workManager.util.WebUtil;
import com.workManager.vo.CostTrendsVo;
import com.workManager.vo.CurrentUserVO;
import com.workManager.vo.ProjectLaborCostVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by strangevy on 2017/5/18
 */
@Controller
@RequestMapping("/projectCost")
public class ProjectCostController extends BaseController {

    @Autowired
    private ProjectCostService projectCostService;


    @ApiOperation(value = "人工成本Excel文件上传接口", notes = "人工成本Excel文件上传接口")
    @RequestMapping(value = "/upLoadExcel", method = RequestMethod.POST)
    public Object upLoadExcel(@RequestParam() MultipartFile file) {
        if (file==null){
            return null;
        }
        ModelMap modelMap = new ModelMap();
        String name = file.getOriginalFilename();
        long size = file.getSize();

        if (name == null || "".equals(name) && size == 0) {
            return null;
        }

        String msg = "上传成功";
        try {
            projectCostService.uploadExcel(name, file);
        } catch (Exception e) {
            return setModelMap(modelMap, RespCode.INTERNAL_SERVER_ERROR, e.getMessage());
        }
        return setModelMap(modelMap, RespCode.OK, msg);
    }

    @ApiOperation(value ="项目人工成本查询",notes = "项目人工成本查询")
    @RequestMapping(value="/countLaberCost",method = RequestMethod.POST)
    /*@RequiresPermissions(value = "/projectCost/countLaberCost")*/
    public Object countLaberCost(HttpServletRequest request,
                                        @RequestParam(value = "pageNumber",required = false) String pageNumber,
                                        @RequestParam(value="pageSize", required = false) String pageSize,
                                        @RequestParam(value = "startTime", required =  false) String startTime,
                                        @RequestParam(value = "endTime", required =  false) String endTime,
                                        @RequestParam(value = "projectCode", required = false) String projectCode,
                                        @RequestParam(value = "projectName", required = false) String projectName,
                                        @RequestParam(value = "loginCode", required = false) String loginCode
                                 ){
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);
        map.put("loginCode",loginCode);

        PageInfo<ProjectLaborCostVO> projectLaborCostVOInfo =  projectCostService.countLaberCost(map);

        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total",projectLaborCostVOInfo.getTotal());
        dataMap.put("rows",projectLaborCostVOInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);

    }

    @ApiOperation(value ="人工成本变化趋势",notes = "人工成本变化趋势查询")
    @RequestMapping(value="/countLaberCostByProjectIds",method = RequestMethod.POST)
    /*@RequiresPermissions(value = "/projectCost/countLaberCostByProjectIds")*/
    public Object countLaberCostByProjectIds(
                                 @RequestParam(value = "projectIds",required = false) List<Integer> projectIds,
                                 @RequestParam(value = "orgId", required =false ) String orgId,
                                 @RequestParam(value = "startTime", required =false ) String startTime,
                                 @RequestParam(value = "endTime", required =false ) String endTime
                                             ){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap();
        map.put("projectIds",projectIds.toArray());
        map.put("orgId",orgId);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        List<CostTrendsVo> list  = projectCostService.countLaberCostByProjectIds(map);

        Map<String,Object> dataMap = new HashMap();
        List<String> monthList =new ArrayList<String>();
        List<BigDecimal> totalAmountList =new ArrayList<BigDecimal>();
        Set<Integer> orgIdSet =new LinkedHashSet   <Integer>();
        Set<String> orgNameSet =new LinkedHashSet   <String>();

        Iterator<CostTrendsVo> iterator = list.iterator();
        while(iterator.hasNext()){
            CostTrendsVo costTrendsVo = iterator.next();

            if(monthList.contains(costTrendsVo.getSalMonth())){
                BigDecimal totalAmount =  totalAmountList.get(totalAmountList.size()-1).add(costTrendsVo.getTotalAmount());
                totalAmountList.remove(totalAmountList.size()-1);
                totalAmountList.add(totalAmount);
            }else{
                monthList.add(costTrendsVo.getSalMonth());
                totalAmountList.add(costTrendsVo.getTotalAmount());
            }

            orgIdSet.add(costTrendsVo.getOrganizationId());
            orgNameSet.add(costTrendsVo.getOrgName());
        }

        dataMap.put("monthList", monthList);
        dataMap.put("totalAmountList", totalAmountList);
        dataMap.put("orgIdSet", orgIdSet);
        dataMap.put("orgNameSet", orgNameSet);

        return  setSuccessModelMap(modelMap,dataMap);
    }
}
